<?php
include "../service/util/authenticateSession.php";
include ("messages.properties.php");
$url = ($_SERVER['REQUEST_URI']);
//|(id=[1-9]{1,}[0-1]*&msg=[5-6])
//if (!preg_match('/^id=[1-9]{1,}[0-1]*$/', substr($url, strrpos($url, "?") + 1, strlen($url))))
//	echo "<script>window.location.href= 'index.php';</script>";
//else if (!preg_match('/^id=[1-9]{1,}[0-1]*&msg=[5-6]*$/', substr($url, strrpos($url, "?") + 1, strlen($url))))
//	echo "<script>window.location.href= 'index.php';</script>";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <title>Chi tiết sự kiện</title>
        <?php
        include("import.php");
        include_once("../service/eventService.php");
        ?>
        <style type="text/css">
            label.vn:hover, label.en:hover { cursor: pointer; }
        </style>
        <script type="text/javascript">
            $(document).ready(function() {
                $("#form").validate({
                    rules: {
                        titleVn: { required: true },
                        titleEn: { required: true }
                    },
					
                    messages: {
                        titleVn: "<?php echo $messages["required"]; ?>",
                        titleEn: "<?php echo $messages["required"]; ?>"
                    }
                });
			
                $('label.en').click(function() {
                    $('#en').toggle("normal");
                });
			
                $('label.vn').click(function() {
                    $('#vn').toggle("normal");
                });
			
                $('#en').toggle("normal");
            });
		
            function validate(){
                return $("#img").val() != "";
            }
        </script>
    </head>
    <body>
        <?php include "header.php"; ?>
        <form id="form" enctype="multipart/form-data">
            <!-- Container -->
            <div id="container">
                <div class="shell">
                    <div id="main">
                        <div class="cl">&nbsp;</div>
                        <!-- Content -->
                        <div id="content">        
                            <?php
                            $message = "";
                            $class = "";
                            if (isset($_GET['msg']))
                                $msg = $_GET['msg'];

                            if (isset($msg)) {
                                if ($msg == "3") {
                                    $message = $messages["updateEventOk"];
                                    $class = "msg msg-ok";
                                }
                                if ($msg == "5") {
                                    $message = $messages["uploadImageServiceOk"];
                                    $class = "msg msg-ok";
                                } else if ($msg == "6") {
                                    $message = $messages["uploadImageServiceFailed"];
                                    $class = "msg msg-error";
                                } else if ($msg == "1"){
                                    $message = $messages["newEventWithoutFlash"];
                                    $class = "msg msg-error";
                                } else if ($msg == "2"){
                                    $message = $messages["newEventWithFlash"];
                                    $class = "msg msg-ok";
                                } else if ($msg == "4"){
                                    $message = $messages["updateEventWithoutFlash"];
                                    $class = "msg msg-ok";
                                }
                            }
                            if ($message == "")
                                $class = 'msg msg-hidden';
                            ?>
                            <div class="<?php echo $class; ?>" id="msg">
                                <p><strong><?php echo $message; ?></strong></p>
                                <a href="#" onclick="hideMessage();" class="close">close</a>
                            </div>
                            <!-- Box -->
                            <div class="box">
                                <!-- Box Head -->
                                <div class="box-head">
                                    <h2>Thông tin sự kiện</h2>
                                </div>
                                <!-- End Box Head -->
                                <!-- Form -->
                                <div class="form">
                                    <?php
                                    if (isset($_GET["id"])) {
                                        $result = findEventById($_GET["id"]);
                                    } else {
                                        $result = findEventById(0);
                                    }

                                    $countEvent = mysql_num_rows($result);
                                    if ($countEvent > 0) {
                                        $row = ConnectionManager::getInstance()->fetch($result);
                                        echo "<input name='id' type='hidden' value='" . $row["EVENT_ID"] . "'/>";
                                    }

                                    $count = 0;
                                    if (isset($_GET["id"])) {
                                        $imagePath = "../images/event/" . $_GET["id"] . "/";
                                        if (is_dir($imagePath)) {
                                            $directory = opendir($imagePath);
                                            $images = array();

                                            while (($file = readdir($directory)) != false) {
                                                if ($file != "." && $file != ".." && $file != ".svn") {
                                                    $images[substr($file, 0, 1)] = $file;
                                                    $count = substr($file, 0, 1);
                                                }
                                            }
                                            closedir($directory);
                                        }
                                    }
                                    ?>
                                    <label class="vn">Tên sự kiện (tiếng Việt)</label>
                                    <div id="vn">
                                        <p id="title-vn">
                                            <input name="titleVn" type="text" class="field size1" value="<?php echo $countEvent > 0 ? $row["EVENT_TITLE_VN"] : ""; ?>"/>
                                        </p>
                                        <p id="content-vn">
                                            <label>Nội dung sự kiện (tiếng Việt)</label>
                                            <textarea id="contentVn" name="contentVn" class="ckeditor"><?php echo $countEvent > 0 ? $row["EVENT_CONTENT_VN"] : ""; ?></textarea>
                                        </p>
                                    </div>
                                    <label class="en">Tên sự kiện (tiếng Anh)</label>
                                    <div id="en">
                                        <p id="title-en">
                                            <input name="titleEn" type="text" class="field size1" value="<?php echo $countEvent > 0 ? $row["EVENT_TITLE_EN"] : ""; ?>"/>
                                        </p>
                                        <p id="content-en">
                                            <label>Nội dung sự kiện (tiếng Anh)</label>
                                            <textarea id="contentEn" name="contentEn" class="ckeditor"><?php echo $countEvent > 0 ? $row["EVENT_CONTENT_EN"] : ""; ?></textarea>
                                        </p>
                                    </div>
                                    <p>
                                        <label>Trạng thái</label>
                                        <select name="status">
                                            <option value="1" <?php if ($countEvent > 0) {
                                        echo $row["EVENT_STATUS"] == 1 ? "selected" : "";
                                    } ?>>Đang chạy</option>
                                            <option value="0" <?php if ($countEvent > 0) {
                                        echo $row["EVENT_STATUS"] == 0 ? "selected" : "";
                                    } ?>>Tạm ngưng</option>
                                        </select>
                                    </p>
                                    <p>
                                        <label>Flash Banner (151 x 459)</label>
                                        <input type="file" name="flash" id="flash" value=""/>
                                    </p>
                                    <p>
                                        <label>Hình ảnh</label>
                                        <input type="file" class="field size1" id="img" name="img"/>
                                        <input type="button" value="Upload" onclick="if(validate()) submitForm('form', '../action/uploadImageEventAction.php?eventId=<?php echo $_GET["id"]; ?>','post');">
                                            <input type="hidden" value="<?php echo $count + 1; ?>" name="nextId">
                                                </p>
                                                <?php
                                                if (isset($_GET["id"]) && is_array($images)) {
                                                    while ($each = current($images)) {
                                                        ?>
                                                        <p><img  src="<?php echo $imagePath . $each; ?>" height="30%" width="30%" align="top" />
                                                            <a href="../action/deleteImageEventAction.php?path=<?php echo $imagePath . $each; ?>&id=<?php echo $_GET["id"]; ?>">X</a></p>
        <?php
        next($images);
    }
}
?>
                                                </div>
                                                <!-- End Form -->
                                                <!-- Form Buttons -->
                                                <div class="buttons">
                                                    <?php
                                                    if ($countEvent <= 0) {
                                                        ?>
                                                        <input type="button" class="button" value="Chấp nhận" onclick="submitForm('form', '../action/addEventAction.php', 'post');" />
                                                        <?php
                                                    } else {
                                                        ?>
                                                        <input type="button" class="button" value="Cập nhật" onclick="submitForm('form', '../action/updateEventAction.php', 'post');" />  
    <?php
}
?>
                                                    <input type="button" class="button" value="Trở về" onclick="window.location = 'event.php';" />
                                                </div>
                                                <!-- End Form Buttons -->
                                                </div>
                                                <!-- End Box -->
                                                </div>
                                                <!-- End Content -->
                                                <div class="cl">&nbsp;</div>			
                                                </div>
                                                <!-- Main -->
                                                </div>
                                                </div>
                                                <!-- End Container -->
                                                </form>
<?php
include "footer.php";
ConnectionManager::getInstance()->closeConnection();
?>
                                                </body>
                                                </html>
